package com.ktjiaoyu.crm.service.impl;

import com.ktjiaoyu.crm.entity.Chance;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.repository.ChanceRepository;
import com.ktjiaoyu.crm.repository.UserRepository;
import com.ktjiaoyu.crm.service.IChanceService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lee
 * 实现类---机会
 */
@Service(value = "chanceService")
public class ChanceServiceImpl implements IChanceService {
    @Resource
    private ChanceRepository chanceRepository;

    @Resource
    private UserRepository userRepository;

    /**
     * 保存机会
     *
     * @param chance 机会
     */
    @Override
    public void save(Chance chance) {
        this.chanceRepository.save(chance);
    }

    /**
     * 删除机会
     *
     * @param chcId ID
     */
    @Override
    public void delete(Long chcId) {
        //查询机会
        Chance chance = chanceRepository.getOne(chcId);
        //删除
        this.chanceRepository.delete(chance);
    }

    /**
     * 查询机会
     *
     * @param chcId ID
     * @return chance
     */
    @Override
    public Chance getChance(Long chcId) {
        return chanceRepository.getOne(chcId);
    }


    /**
     * 分页查询
     * @param chcCustName 客户名称
     * @return 集合
     */
    @Override
    public Page<Chance> findAllChance(String chcCustName, String chcTitle,
                                      String chcLinkman, String chcStatus,
                                      Long chcCreateId, Long chcDueId, Pageable pageable) {
        Specification<Chance> chanceSpecification = new Specification<Chance>() {
            @Override
            public Predicate toPredicate(Root<Chance> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (chcCustName!=null && ! chcCustName.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcCustName"), "%"+chcCustName+"%"));
                }
                if (chcTitle!=null && !chcTitle.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcTitle"), "%"+chcTitle+"%"));
                }
                if (chcLinkman!=null && !chcLinkman.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcLinkman"), "%"+chcLinkman+"%"));
                }
                if (chcStatus!=null && !chcStatus.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcStatus"), "%"+chcStatus+"%"));
                }
                if (chcCreateId!=null && chcCreateId!=0){
                    predicates.add(criteriaBuilder.equal(root.get("chcCreateId"),chcCreateId));
                }
                if (chcDueId!=null && chcDueId!=0){
                    predicates.add(criteriaBuilder.equal(root.get("chcDueId"), chcDueId));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return chanceRepository.findAll(chanceSpecification, pageable);
    }

    /**
     * 修改销售机会
     *
     * @param chance 机会
     */
    @Override
    public void edit(Chance chance) {
        this.chanceRepository.edit(chance);
    }

    /**
     * 修改状态
     *
     * @param chcStatus 机会状态
     * @param chcId     机会Id
     */
    @Override
    public void updateChcStatusByChcId(String chcStatus, Long chcId) {
        this.chanceRepository.updateChcStatusByChcId(chcStatus, chcId);
    }

    /**
     * 通过角色查找用户
     *
     * @param roleId 角色Id
     */
    @Override
    public List<User> findUsersByRoleId(Long roleId) {
       return userRepository.findUsersByRoleRoleId(roleId);
    }

    /**
     * 销售主管查询已分配的销售机会
     *
     * @param chcCustName 客户名称
     * @param chcTitle    概要
     * @param chcLinkman  联系人
     * @param chcDueId    指派的ID
     * @param pageable    分页条件
     * @return page
     */
    @Override
    public Page<Chance> findChanceByChcDueId(String chcCustName, String chcTitle, String chcLinkman,
                                             Long chcDueId,
                                             Pageable pageable) {
        Specification<Chance> chanceSpecification = new Specification<Chance>() {
            @Override
            public Predicate toPredicate(Root<Chance> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (chcCustName!=null && ! chcCustName.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcCustName"), "%"+chcCustName+"%"));
                }
                if (chcTitle!=null && !chcTitle.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcTitle"), "%"+chcTitle+"%"));
                }
                if (chcLinkman!=null && !chcLinkman.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcLinkman"), "%"+chcLinkman+"%"));
                }
                if (chcDueId!=null && chcDueId!=0){
                    predicates.add(criteriaBuilder.equal(root.get("chcDueId"), chcDueId));
                }
                predicates.add(criteriaBuilder.notLike(root.get("chcStatus"), "%未分配%"));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return this.chanceRepository.findAll(chanceSpecification, pageable);
    }
}
